package org.apache.james.mime4j.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;

public class EOLConvertingInputStream extends InputStream
{
  public static final int CONVERT_BOTH = 3;
  public static final int CONVERT_CR = 1;
  public static final int CONVERT_LF = 2;
  private int flags;
  private PushbackInputStream in;
  private int previous;

  public EOLConvertingInputStream(InputStream paramInputStream)
  {
    this(paramInputStream, 3);
  }

  public EOLConvertingInputStream(InputStream paramInputStream, int paramInt)
  {
    this.in = null;
    this.previous = 0;
    this.flags = 3;
    this.in = new PushbackInputStream(paramInputStream, 2);
    this.flags = paramInt;
  }

  public void close()
    throws IOException
  {
    this.in.close();
  }

  public int read()
    throws IOException
  {
    int j;
    int i = this.in.read();
    if (i == -1)
    {
      j = -1;
      return j;
    }
    if (((0x1 & this.flags) != 0) && (i == 13))
    {
      int k = this.in.read();
      if (k != -1)
        this.in.unread(k);
      if (k != 10)
        this.in.unread(10);
    }
    while (true)
    {
      do
        while (true)
        {
          this.previous = i;
          j = i;
        }
      while (((0x2 & this.flags) == 0) || (i != 10) || (this.previous == 13));
      i = 13;
      this.in.unread(10);
    }
  }
}